import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

import { lunbo, recommend, user ,tui,pinglun,canshu,muen} from "./data/data.js"

export default {
	bootstrap() {

		console.log("mock 开始实行")

		var mock = new MockAdapter(axios);
		let data = lunbo;
		// 获取轮播信息
		mock.onGet('/info').reply(200, {
			msg: data
		});
		//这是今日推荐
		mock.onGet('/recomend').reply(200, {
			msg: recommend
		});
		
		mock.onGet('/menu').reply(200, {
			msg: muen
		});
		//这是登陆
		mock.onPost('/login').reply(config => {
			let {tel,password} = JSON.parse(config.data);
			
			return new Promise((resolve, reject) => {
		       let u = null;
		        setTimeout(() => {
		          let hasUser = user.some(us => {
		            if (us.tel === tel && us.password === password) {
		              u = JSON.parse(JSON.stringify(us));
		              u.password = undefined;
		              return true;
		            }
		          });
		
		          if (hasUser) {
		            resolve([200, { code: 200, msg: '请求成功', u }]);
		          } else {
		            resolve([200, { code: 500, msg: '账号或密码错误' }]);
		          }
		        }, 1000);
		      });

			

		});
		//获取评论
	    mock.onGet('/pinglun').reply(200,{
	    	msg: pinglun
	    })
	    //获取参数
	    mock.onGet('/canshu').reply(200,{
	    	msg: canshu
	    })
	mock.onGet('/tui').reply(200,{
			msg:tui
		});
		//这是注册
		mock.onPost('/register').reply(config => {
			let {tel,password} = JSON.parse(config.data);	
			return new Promise((resolve, reject) => {
		       let u = null;
		        setTimeout(() => {
		          let hasUser = user.some(us => {
		            if (us.tel == tel && us.password == password) {
		              
		              return true;
		            }
		          });
		          if (hasUser) {
		            resolve([200, { code: 500, msg: '已经被注册' }]);
		          } else {
		          	let adduser={tel:tel,password:password,name:"未知",avatar:""}
		          	user.push(adduser);
		            resolve([200, { code: 200, msg: '注册成功' }]);
		          }
		        }, 1000);
		      });
		});
	}
}